home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / SAT 2.3b4 / Demo ƒ / myPlatform demo ƒ / sPlatform.p < prev    next >
Text File  |  1995-01-16  |  3KB  |  130 lines

  1. { Platform sprite, experimental faceless sprite }
  2.  
  3. unit sPlatform;
  4.  
  5. interface
  6.  
  7.     uses
  8. {$IFC UNDEFINED THINK_PASCAL}
  9.         Types, Quickdraw, 
  10. {$ENDC}
  11.         SAT;
  12.  
  13.     procedure InitPlatform;
  14.     procedure SetupPlatform (me: SpritePtr);
  15.     procedure HandlePlatform (me: SpritePtr);
  16.     procedure HitPlatform (me, him: SpritePtr);
  17.  
  18. implementation
  19.  
  20.     procedure InitPlatform;
  21.         var
  22.             i: integer;
  23.     begin
  24. {The platform has no faces!}
  25.     end;
  26.  
  27.     procedure SetupPlatform (me: SpritePtr);
  28.         var
  29.             r: Rect;
  30.             pol: PolyHandle;
  31.     begin
  32.         me^.task := @HandlePlatform;
  33.         me^.hittask := @HitPlatform;
  34.  
  35.         {me^.kind := -2; {Enemy kind}
  36.         me^.face := nil; {=faceless!}
  37.         SetRect(me^.hotRect, 0, 0, 100, 16);
  38.         r := me^.hotRect;
  39.         offSetRect(r, me^.position.h, me^.position.v);
  40.         SATSetPortBackScreen;
  41. {Perhaps we should SATSetPortBackScreen nowadays…}
  42. {but it works anyway - we're in no hurry here, and we don't CopyBits.}
  43. {$IFC UNDEFINED THINK_PASCAL}
  44.         FillRect(r, qd.dkgray);
  45. {$ELSEC}
  46.         FillRect(r, dkgray);
  47. {$ENDC}
  48.  
  49.         pol := OpenPoly;
  50.         MoveTo(r.left, r.top);
  51.         LineTo(r.left + 5, r.top - 5);
  52.         LineTo(r.right + 5, r.top - 5);
  53.         LineTo(r.right, r.top);
  54.         LineTo(r.left, r.top);
  55.         LineTo(r.right, r.top);
  56.  
  57.         LineTo(r.right, r.bottom);
  58.         LineTo(r.right + 5, r.bottom - 5);
  59.         LineTo(r.right + 5, r.top - 5);
  60.         LineTo(r.right, r.top);
  61.  
  62.         ClosePoly;
  63.         ErasePoly(pol);
  64.         FramePoly(pol);
  65.         KillPoly(pol);
  66.  
  67.         r.top := r.top - 5;
  68.         r.right := r.right + 5;
  69.         SATBackChanged(r); {Tell SAT to draw it when appropriate}
  70.  
  71.         me^.layer := -me^.position.v;
  72.     end;
  73.  
  74.     procedure HandlePlatform (me: SpritePtr);
  75.     begin
  76.         {me^.face := nil; {Really not needed}
  77.     end;
  78.  
  79.     procedure HitPlatform;
  80.         var
  81.             mini, i, min: integer;
  82.             diff: array[1..4] of integer;
  83.     begin
  84.         if him^.Task <> @HandlePlatform then
  85.             begin
  86.                 diff[1] := -me^.hotRect2.top + (him^.hotRect2.bottom);    {TtoB}
  87.                 diff[2] := -him^.hotRect2.top + (me^.hotRect2.bottom);    {BtoT}
  88.                 diff[3] := -me^.hotRect2.left + (him^.hotRect2.right);    {LtoR}
  89.                 diff[4] := -him^.hotRect2.left + (me^.hotRect2.right);    {RtoL}
  90.                 mini := 0;
  91.                 min := 10000;
  92.                 for i := 1 to 4 do
  93.                     if min > diff[i] then
  94.                         begin
  95.                             min := diff[i];
  96.                             mini := i;
  97.                         end;
  98.                 case mini of
  99.                     1: {floor}
  100.                         begin
  101.                             him^.position.v := him^.position.v - diff[1] + 1;
  102.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  103.                             if him^.speed.v > 0 then
  104.                                 him^.speed.v := 0;
  105.                         end;
  106.                     2: {ceiling}
  107.                         begin
  108.                             him^.position.v := him^.position.v + diff[2] + 1;{me^.position.v + 17}
  109. {We don't signal here. A hit in the ceiling should just send him back down again.}
  110.                             if him^.speed.v < 0 then
  111.                                 him^.speed.v := -him^.speed.v;
  112.                         end;
  113.                     3: {left}
  114.                         begin
  115.                             him^.position.h := him^.position.h - diff[3] - 1;{me^.position.h - 32}
  116.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  117.                             if him^.speed.h > 0 then
  118.                                 him^.speed.h := -him^.speed.h;
  119.                         end;
  120.                     4: {right}
  121.                         begin
  122.                             him^.position.h := him^.position.h + diff[4] + 1;{me^.position.h + 100}
  123.                             him^.kind := 10; {Signal to him, as if we used KindCollision}
  124.                             if him^.speed.h < 0 then
  125.                                 him^.speed.h := -him^.speed.h;
  126.                         end;
  127.                 end;{case}
  128.             end; {if}
  129.     end; {HitPlatform}
  130. end.{of unit}